# -------------------------------------------------------------------
# Purpose: Creates Figure B5
# Author:  Max Posch, 25/07/2025
# Usage:   Source this script to generate the figure
# -------------------------------------------------------------------
# Check that required paths exist
stopifnot(dir.exists(pdataconfanalysis))
stopifnot(dir.exists(poutputappendix))


## Load data
immigr <- fread(file.path(pdataconfanalysis, "immigrationNameLevel1900BoundariesPanel18801930.csv"))


# Plot for top 10 surnames
top_names_year <- c("SMITH", "JOHNSON", "ANDERSON", "COHEN", "MILLER", "NELSON", "GARCIA", "MARTINEZ", "BROWN", "LOPEZ")
temp <- unique(immigr[namelast %in% top_names_year, .(namelast, year, I_n_adjp, I_adjp)])
temp[, share_immig := I_n_adjp / I_adjp]
temp[, namelast := factor(namelast, levels = top_names_year, ordered = TRUE)]

plotname <- file.path(poutputappendix, "figureB05.pdf")
p <- ggplot(temp, aes(x = year, y = share_immig * 100, group = namelast)) +
        geom_line() +
        geom_point() +
        facet_grid(namelast ~ ., scales = "free_y") +
        labs(
                x = "Year",
                y = "Share of immigrants (in %)"
        ) +
        theme_minimal() +
        ylim(0, NA)
ggsave(plotname, p, width = 8, height = 9.2)
cat("Figure saved to:", plotname, "\n")

